/*
 * adjacencylist.c
 *
 *  Created on: Jun 17, 2010
 *      Author: Guilherme
 */

#include "adjacencylist.h"

AdjacencyList initAdjacencyList(int numberNodes) {
	AdjacencyList L;

	if ((L = malloc(sizeof(struct adjacencyListStruct))) == NULL) {
		fprintf(stderr, "Could not allocate memory for a new Adjacency List");
		exit(2);
	}
	if ((L->nodes = calloc((numberNodes + 1), sizeof(struct node *))) == NULL) {
		fprintf(stderr, "Could not allocate memory for Linked List nodes");
		exit(2);
	}
	L->numberNodes = numberNodes;
	int i;

	for (i = 1; i <= numberNodes; i++) {
		L->nodes[i] = append_node(L->nodes[i], i, INT_MAX);
	}

	return L;
}

AdjacencyList addNeighbor(AdjacencyList L, int nodeID, int neighborID, int linkCost) {

	L->nodes[nodeID] = append_node(L->nodes[nodeID], neighborID, linkCost);

	return L;
}

LinkedList getNeighbor(AdjacencyList L, int nodeID) {
	return remove_node(L->nodes[nodeID]);
}

void destroy_adjacency_list(AdjacencyList L) {
	int i;

	for (i = 0; i <= L->numberNodes; i++) {
		// clear linked list
		L->nodes[i] = empty(L->nodes[i]);
		//L->nodes[i] = NULL;
	}
	free(L->nodes);
	// clear adjacency list
	free(L);
}

/*int main(int argc, char **argv) {
	AdjacencyList L;
	LinkedList E;

	L = initAdjacencyList(4);
	L = addNeighbor(L, 1, 2, 0);
	L = addNeighbor(L, 1, 3, 0);
	L = addNeighbor(L, 1, 4, 0);
	print_list(L->nodes[1]);
	E= getNeighbor(L, 1);
	E= getNeighbor(L, 1);
	E= getNeighbor(L, 1);
	print_list(L->nodes[1]);

	return 0;
}*/

